package com.antler.smt.admin.controller;

import com.antler.smt.entity.PermissionEntity;
import com.antler.smt.enums.PermissionTypeEnum;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.exception.VerificationException;
import com.antler.smt.request.admin.AddPermissionReq;
import com.antler.smt.request.admin.GetPermissionListReq;
import com.antler.smt.request.admin.UpdatePermissionReq;
import com.antler.smt.response.admin.GetPermissionTreeResp;
import com.antler.smt.response.admin.ListPermissionResp;
import com.antler.smt.response.admin.ZtreeResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.admin.PermissionService;
import com.antler.smt.validate.DataValidator;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限Controller
 *
 * @author vic
 * @date 2018-04-15 7:20
 */
@RestController
@RequestMapping(value = "/permission")
@Api(description = "权限接口")
public class PermissionController extends BaseController {

    /**
     * 权限接口
     */
    @Autowired
    private PermissionService permissionService;

    /**
     * 获取权限树
     *
     * @return Result实体
     */
    @GetMapping("/treePerms")
    @ApiOperation(value = "获取权限树", httpMethod = "GET")
    public Result<List<ZtreeResp>> treePerms() {
        return permissionService.treePerms();
    }

    /**
     * 获取权限列表
     *
     * @param request 请求实体
     * @return Result实体
     */
    @GetMapping("/listRoleInfo")
    @ApiOperation(value = "获取权限列表", httpMethod = "GET")
    public Result<PageInfo<ListPermissionResp>> listPermissionInfo(GetPermissionListReq request) {
        DataValidator.isNull(request, "获取权限列表接口，请求参数不能为空！");
        return permissionService.listPermissionInfo(request);
    }

    /**
     * 新增权限
     *
     * @param request 请求实体
     * @return Result实体
     */
    @PostMapping("/addPermission")
    @ApiOperation(value = "新增权限", httpMethod = "POST")
    public Result addPermission(@RequestBody AddPermissionReq request) {
        DataValidator.isNull(request, "新增权限接口，请求参数不能为空！");
        DataValidator.isBlank(request.getName(), "权限名称必传！");
        //默认上级菜单
        if (request.getParentId() == null) {
            request.setParentId(0L);
        }
        DataValidator.isNull(request.getType(), "权限类型必传！");
        //region 验证合法性
        PermissionEntity permissionEntity = new PermissionEntity();
        permissionEntity.setParentId(request.getParentId());
        permissionEntity.setType(request.getType());
        verifyParentId(permissionEntity);
        //endregion
        return permissionService.addPermission(request);
    }

    /**
     * 更新权限
     *
     * @param request 请求实体
     * @return Result实体
     */
    @PostMapping("/updatePermission")
    @ApiOperation(value = "更新权限", httpMethod = "POST")
    public Result updatePermission(@RequestBody UpdatePermissionReq request) {
        DataValidator.isNull(request, "更新权限接口，请求参数不能为空！");
        DataValidator.isBlank(request.getName(), "权限名称必传！");
        DataValidator.isNull(request.getParentId(), "上级权限id必传！");
        DataValidator.isNull(request.getType(), "权限类型必传！");
        //region 验证合法性
        PermissionEntity permissionEntity = new PermissionEntity();
        permissionEntity.setParentId(request.getParentId());
        permissionEntity.setType(request.getType());
        verifyParentId(permissionEntity);
        //endregion
        return permissionService.updatePermission(request);
    }

    /**
     * 删除权限
     *
     * @param permissionId 权限id
     * @return Result实体
     */
    @PostMapping("/deletePermission")
    @ApiOperation(value = "删除权限", httpMethod = "POST")
    public Result deletePermission(@ApiParam(name = "permissionId", value = "权限id", required = true) @RequestParam Long permissionId) {
        DataValidator.isNull(permissionId, "更新权限接口，权限id不能为空！");
        return permissionService.deletePermission(permissionId);
    }

    /**
     * 获取用户权限列表
     *
     * @param userId 用户id
     * @return Result实体
     */
    @GetMapping("/getUserPermissionList")
    @ApiOperation(value = "获取用户权限列表", httpMethod = "GET")
    public Result<List<ListPermissionResp>> getUserPermissionList(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam Long userId) {
        DataValidator.isNull(userId, "获取用户权限接口，用户id不能为空！");
        List<GetPermissionTreeResp> userPermissionList = permissionService.getUserPermissionList(userId);
        return new Result(userPermissionList);
    }

    /**
     * 选择权限
     *
     * @return Result实体
     */
    @GetMapping("/selectPermission")
    @ApiOperation(value = "选择权限", httpMethod = "GET")
    public Result<List<ZtreeResp>> selectPermission() {
        Result<List<ZtreeResp>> selectZTreeResp = permissionService.queryNotButtonList();
        return selectZTreeResp;
    }

    /**
     * 验证权限上级节点id
     *
     * @param permissionEntity 权限实体
     */
    private void verifyParentId(PermissionEntity permissionEntity) {
        if (permissionEntity != null) {
            //上级菜单类型
            int parentType = PermissionTypeEnum.CATALOG.key();
            if (permissionEntity.getParentId() != 0) {
                PermissionEntity parentPermissionEntity = permissionService.getPermissionEntity(permissionEntity.getParentId());
                parentType = parentPermissionEntity.getType();
            }
            //目录、菜单
            if (permissionEntity.getType() == PermissionTypeEnum.CATALOG.key().byteValue() || permissionEntity.getType() == PermissionTypeEnum.MENU.key().byteValue()) {
                if (parentType != PermissionTypeEnum.CATALOG.key()) {
                    throw new VerificationException(ResultCodeEnum.QUERY_DATA_ERROR, "上级菜单只能为目录类型！");
                }
            }
            if (permissionEntity.getType() == PermissionTypeEnum.BUTTON.key().byteValue()) {
                if (parentType != PermissionTypeEnum.MENU.key()) {
                    throw new VerificationException(ResultCodeEnum.QUERY_DATA_ERROR, "上级菜单只能为菜单类型！");
                }
            }
        }
    }
}
